<?php

namespace app\api\Service;

use app\common\library\Auth;
use app\common\model\Area;
use think\Config;
use think\Db;

class MasterHomeService
{
    // 获取轮播图
    public static function getBanner()
    {
        $list = \app\admin\model\Banner::where(['type'=>3,'status'=>'normal'])
            ->order('weigh desc')->field('id,image,content')->select();
        return ['code' =>1,'msg'=>'查询成功','data'=>$list];
    }

    // 获取轮播图
    public static function getPage($data)
    {
        $id = $data['id'];
        $details = \app\admin\model\Page::where(['id'=>$id])
            ->order('id desc')->field('id,title,content')->find();
        return ['code' =>1,'msg'=>'查询成功','data'=>$details];
    }

    // 实名认证
    public static function putRealname($data)
    {
        // 实例化认证类以获取当前用户信息
        $auth = Auth::instance();
        // 获取当前用户的ID
        $user_id = $auth->id;

        $realnameInfo = \app\admin\model\MasterRealname::where(['user_id'=>$user_id])->find();
        if ($realnameInfo && !$realnameInfo['status'] == 0) return ['code' =>0,'msg'=>'您已申请实名认证请等待审核','data'=>''];
        if ($realnameInfo && !$realnameInfo['status'] == 1) return ['code' =>0,'msg'=>'您已实名认证!!','data'=>''];

        // 获取要添加到购物车的商品ID
        $province_id = $data['province_id'];
        $city_id = $data['city_id'];
        $area_id = $data['area_id'];
        $realname = $data['realname'];
        $duration = $data['duration'];
        $is_install = $data['is_install'];
        $idnumber = $data['idnumber'];
        $idcard_r = $data['idcard_r'];
        $idcard_g = $data['idcard_g'];
        $electrician = isset($data['electrician']) ? $data['electrician'] : '';
        if(!$province_id) return ['code' =>0,'msg'=>'请选择省份','data'=>''];
        if(!$city_id) return ['code' =>0,'msg'=>'请选择城市','data'=>''];
        if(!$area_id) return ['code' =>0,'msg'=>'请选择地区','data'=>''];
        if(!$realname) return ['code' =>0,'msg'=>'请输入姓名','data'=>''];
        if(!$duration) return ['code' =>0,'msg'=>'请输入工作年限','data'=>''];
        if($is_install != 0 && $is_install != 1) return ['code' =>0,'msg'=>'请选择是否安装过充电桩','data'=>''];
        if(!$idnumber) return ['code' =>0,'msg'=>'请输入身份证号','data'=>''];
        if(!$idcard_r) return ['code' =>0,'msg'=>'请上传身份证人像面','data'=>''];
        if(!$idcard_g) return ['code' =>0,'msg'=>'请上传身份证国徽面','data'=>''];

        // 开始数据库事务
        try{
            $params = [
                'user_id' => $user_id,
                'realname' => $realname,
                'duration' => $duration,
                'is_install' => $is_install,
                'idnumber' => $idnumber,
                'idcard_r' => $idcard_r,
                'idcard_g' => $idcard_g,
                'electrician' => $electrician,
                'province_id' => $province_id,
                'city_id' => $city_id,
                'area_id' => $area_id,
                'status' => 0,
            ];

            $res = \app\admin\model\MasterRealname::create($params);

            // 提交事务
            Db::commit();
            // 根据操作结果返回相应的消息
            if($res){
                // 向后台推送消息
                $content = "电工{$realname}申请实名认证";
                pushNotice($content, '', $user_id);
                return ['code'=>1,'msg'=>'操作成功','data'=>''];
            }else{
                return ['code'=>0,'msg'=>'操作失败','data'=>''];
            }
        }catch(\Exception $e){
            // 如果发生异常，则回滚事务
            Db::rollback();
            // 返回异常信息
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
    }

    // 运营中心列表-到城市
    public static function getOperation($data)
    {
        $level = isset($data['level']) ? $data['level'] : 1;
        $pid = isset($data['pid']) ? $data['pid'] : 0;


        $list = Area::where(['pid'=>$pid,'level'=>$level])->field('id,name')->select();
        if($level == 2){
            foreach ($list as &$item){
                $operation = \app\admin\model\Operation::where(['city_id'=>$item['id']])->field('title,image,content')->find();
                if($operation){
                    $item['title'] = $operation['title'];
                    $item['image'] = $operation['image'];
                    $item['content'] = $operation['content'];
                }else{
                    $item['title'] = '招募运营商';
                    $item['image'] = '';
                    $item['content'] = Config::get('site.recruit');
                }
            }
        }
        return ['code'=>1,'msg'=>'操作成功','data'=>$list];
    }

    // 获取轮播图
    public static function getPhone()
    {
        $deta = Config::get("site.400phone");
        return ['code' =>1,'msg'=>'查询成功','data'=>$deta];
    }

    // 我要签约
    public static function putSign($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        
        $realname = \app\admin\model\MasterRealname::where(['user_id'=>$user_id])->find();
        if(!$realname) return ['code'=>0,'msg'=>'请先实名认证','data'=>''];
        if($realname['status'] != 1) return ['code'=>0,'msg'=>'请实名认证通过以后再申请签约','data'=>''];
        
        
        $result = \app\admin\model\User::where(['id'=>$user_id])->update(['is_sign'=>1]);
        if ($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }
    
    // 更新经纬度
    public static function updateLocation($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;

        $result = \app\admin\model\User::where(['id'=>$user_id])->update(['lat'=>$data['lat'],'lng'=>$data['lng'],'updatetime'=>time()]);
        if ($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
       }
    }

}
